package com.mhkj.base.calender.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.calender.entity.WorkCalender;
import com.mhkj.base.calender.repository.WorkCalenderRepository;
import com.mhkj.base.calender.service.WorkCalenderService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

/**
 * 工作日历
 * @author mics
 * @date 2018年7月2日
 * @version 1.0
 */
@Service
public class WorkCalenderServiceImpl extends BaseServiceImpl<WorkCalender> implements WorkCalenderService{
	
	@Resource
	private WorkCalenderRepository calenderRepository;
	@Resource
	private BusinessTypeService businessTypeService;
	
	@Resource
	public void setCalenderRepository(WorkCalenderRepository calenderRepository) {
		super.setBaseRepository(calenderRepository);
	}

	@Override
	public Pagination<WorkCalender> listByPage(WorkCalender workCalender,
			int pageIndex, int size) {
		Specification<WorkCalender> spec = new Specification<WorkCalender> () {  
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<WorkCalender> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              if(workCalender.getType() != 0){
                  Predicate p1=criteriaBuilder.like(root.get("type").as(String.class), "%"+workCalender.getType()+"%");
                  lstPredicates.add(p1);
              }
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<WorkCalender> pageT =  calenderRepository.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC,"id"));
		Pagination<WorkCalender> pagination = new Pagination<WorkCalender>();
		pagination.setTotal(calenderRepository.count(spec));
		List<WorkCalender> workCalenders = pageT.getContent();
		pagination.setData(workCalenders);
		return pagination;
	}

	@Override
	public int calsDeadline(Date beginDate, Date endDate) {
		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		int result = 0;//正常工作日
		while (beginCalendar.compareTo(endCalendar)<=0) {
			if (beginCalendar.get(Calendar.DAY_OF_WEEK)!= 7 && beginCalendar.get(Calendar.DAY_OF_WEEK)!= 1){
				result++;
			}
			beginCalendar.add(Calendar.DATE, 1);
		}
		int workflag = 0;//特殊工作日
		workflag = calenderRepository.findSepcialWork(beginDate,endDate);
		int noWorkFlag = 0;//非工作日
		noWorkFlag = calenderRepository.findSpecialNoWork(beginDate,endDate);
		int count =  result+workflag-noWorkFlag;//总共天数
		return count;
	}
	
}
